<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    for (var i = 1; i <= 3; i++) {  //建议使用let 可正常输出i的值
        setTimeout(function () {
            console.log(i);
        }, 0);
    };
    var a = 1;
    function test() {
        a = 2
    }
    test()
    console.log(a);//2
    // 如果test函数里面的是var a=2就相当于定义全局变量，这个时候a就是1了
</script>

</html>